package demo.hdz.testui;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

/**
 * @author hdz
 * @date 2018/07/12
 */
public class UiAdaptActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCustomDensity(this, getApplication());
        setContentView(R.layout.activity_ui_adapt);

        RadioButton rBtnCarPlay = (RadioButton)findViewById(R.id.rBtnCarPlay);
        Drawable drawableCarPlay=getResources().getDrawable(R.drawable.carplay);
        drawableCarPlay.setBounds(0,0,dp2px(this,20),dp2px(this,20));
        rBtnCarPlay.setCompoundDrawables(drawableCarPlay,null,null,null);

        RadioButton rBtnCarLife= (RadioButton)findViewById(R.id.rBtnCarLife);
        Drawable drawableCarLife = getResources().getDrawable(R.drawable.carlife);
        drawableCarLife.setBounds(0,0,dp2px(this,20),dp2px(this,20));
        rBtnCarLife.setCompoundDrawables(drawableCarLife,null,null,null);

        RadioButton rBtnAndroidAuto = (RadioButton)findViewById(R.id.rBtnAndroidAuto);
        Drawable drawableAndroidAuto=getResources().getDrawable(R.drawable.androidauto);
        drawableAndroidAuto.setBounds(0,0,dp2px(this,20),dp2px(this,20));
        rBtnAndroidAuto.setCompoundDrawables(drawableAndroidAuto,null,null,null);

        Button btnBack = (Button) findViewById(R.id.btnBack);
        Drawable drawableBack=getResources().getDrawable(R.drawable.back_button_n);
        drawableBack.setBounds(0,0,dp2px(this,10),dp2px(this,14));
        btnBack.setCompoundDrawables(drawableBack,null,null,null);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * dp转换成px
     */
    private int dp2px(Context context, float dpValue){
        float scale=context.getResources().getDisplayMetrics().density;
        return (int)(dpValue*scale+0.5f);
    }

    // 今日头条的屏幕适配方案
    // 通过修改density值，强行把所有不同尺寸分辨率的手机的宽度dp值改成一个统一的值，这样就解决了所有的适配问题
    // @param activity
    // @param application
    private static float sNoncompatDensity = 0;
    private static float sNoncompatScaledDensity = 0;
    public static void setCustomDensity(Activity activity, final Application application){
        DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();
        if (sNoncompatDensity == 0){
            sNoncompatDensity       = appDisplayMetrics.density;
            sNoncompatScaledDensity = appDisplayMetrics.scaledDensity;
            application.registerComponentCallbacks(new ComponentCallbacks() {
                @Override
                public void onConfigurationChanged(Configuration newConfig) {
                    if (newConfig!=null && newConfig.fontScale>0){
                        sNoncompatScaledDensity = application.getResources().getDisplayMetrics().scaledDensity;
                    }
                }

                @Override
                public void onLowMemory() {

                }
            });
        }
        float targetDensity      = (float)appDisplayMetrics.widthPixels/320;
        float targetScaleDensity = targetDensity*(sNoncompatScaledDensity/sNoncompatDensity);
        int targetDensityDpi    = (int)(160*targetDensity);
        appDisplayMetrics.density       = targetDensity;
        appDisplayMetrics.scaledDensity = targetScaleDensity;
        appDisplayMetrics.densityDpi    = targetDensityDpi;

        final DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
        activityDisplayMetrics.density       =targetDensity;
        activityDisplayMetrics.scaledDensity =targetScaleDensity;
        activityDisplayMetrics.densityDpi    =targetDensityDpi;
    }
}
